package com.jeecg.p3.system.advisor.service;

import com.jeecg.p3.system.advisor.dao.StudyAdviserDao;
import com.jeecg.p3.system.advisor.entity.StuAdvGroup;
import com.jeecg.p3.system.advisor.entity.StudyAdviser;
import com.jeecg.p3.system.dao.JwSystemUserDao;
import com.jeecg.p3.system.dao.JwWebJwidDao;
import com.jeecg.p3.system.entity.JwSystemUser;
import com.jeecg.p3.system.util.Constants;
import com.jeecg.p3.system.vo.LoginUser;
import org.jeecgframework.p3.core.util.MD5Util;
import org.jeecgframework.p3.core.utils.common.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * TODO
 * Created on 2020-08-29$ 9:10$
 *
 * @author 张晓$
 * @version V1.0
 */
@Service("studyAdvService")
public class StudyAdvService {
    @Resource
    private StudyAdviserDao adviserDao;//顾问处理
    @Resource
    private JwSystemUserDao jwSystemUserDao;//用户处理
    @Resource
    private JwWebJwidDao jwWebJwidDao;//公众号授权处理

    /**
     * 分页查询学习顾问
     * @param pageQuery
     * @return
     */
    public PageList<StudyAdviser> queryPageList(PageQuery<StudyAdviser> pageQuery) {
        PageList<StudyAdviser> result = new PageList<StudyAdviser>();
        Integer itemCount = adviserDao.count(pageQuery);
        PageQueryWrapper<StudyAdviser> wrapper = new PageQueryWrapper<StudyAdviser>(pageQuery.getPageNo(), pageQuery.getPageSize(),itemCount, pageQuery.getQuery());
        List<StudyAdviser> list = adviserDao.queryPageList(wrapper);
        Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount, pageQuery.getPageSize());
        result.setPagenation(pagenation);
        result.setValues(list);
        return result;
    }

    /**
     * 新增学习顾问
     * @param adviser
     */
    @Transactional(rollbackFor = {Exception.class})
    public void doAdd(StudyAdviser adviser) {
        if(StringUtils.isNotEmpty(adviser.getPassword())) {
            String passwordEncode = MD5Util.MD5Encode(adviser.getPassword(), "utf-8");
            adviser.setPassword(passwordEncode);
        }

        JwSystemUser user = convertUser(adviser);
        jwSystemUserDao.insert(user);//新增操作员
        jwSystemUserDao.insertUserRoleRel(user.getUserId(), Constants.stuAdvRole);//新增用户角色为顾问
        jwWebJwidDao.insertUserJwidRels(user.getUserId(), Constants.wxid);//新增用户公众号授权
        adviserDao.insert(adviser);
    }

    /**
     * 顾问转用户方法
     * @param adviser
     * @return
     */
    public JwSystemUser convertUser(StudyAdviser adviser) {
        JwSystemUser user = new JwSystemUser();
        user.setUserId(adviser.getUserNumber());
        user.setPassword(adviser.getPassword());
        user.setAdmin("0");//设置类型为学习顾问
        user.setUserName(adviser.getRealName());
        user.setState("0");//设置顾问未激活
        user.setUserStat(Constants.USER_NORMAL_STATE);
        user.setCreateDt(new Date());
        return user;
    }

    /**
     * 根据参数获取顾问列表
     * @param adviser 学习顾问参数
     * @return
     */
    public List<StudyAdviser> findByParam(StudyAdviser adviser) {
        if(adviser == null) {
            adviser = new StudyAdviser();
        }
        return adviserDao.findByParam(adviser);
    }

    /**
     * 根据id查询顾问信息
     * @param id
     * @return
     */
    public StudyAdviser findById(Long id) {
        return adviserDao.get(id);
    }

    /**
     * 修改顾问基本信息
     * @param adviser
     */
    @Transactional(rollbackFor = {Exception.class})
    public void doEdit(StudyAdviser adviser) {
        if(StringUtils.isNotEmpty(adviser.getPassword())) {
            String passwordEncode = MD5Util.MD5Encode(adviser.getPassword(), "utf-8");//密码加密
            adviser.setPassword(passwordEncode);
        }

        JwSystemUser user = jwSystemUserDao.findByUserId(adviser.getUserNumber());
        if(user != null && user.getId() != null && StringUtils.isNotEmpty(user.getId().toString())) {
            user.setPassword(adviser.getPassword());
            user.setUserName(adviser.getRealName());
            jwSystemUserDao.update(user);//更新用户状态为失效
        }

        adviserDao.update(adviser);
    }

    /**
     * 顾问激活
     * @param adviser
     * @param uid
     */
    @Transactional(rollbackFor = {Exception.class})
    public void update(StudyAdviser adviser, Long uid) {
        adviser.setIsAct("1");//设置顾问已激活
        JwSystemUser user = jwSystemUserDao.get(uid);
        user.setState("1");//设置状态为已激活
        user.setOpenid(adviser.getOpenId());//设置用户微信openid
        user.setUserIcon(adviser.getPhoto());//设置用户头像
        jwSystemUserDao.update(user);
        adviserDao.update(adviser);
    }

    /**
     * 顾问删除
     * @param adviser
     */
    @Transactional(rollbackFor = {Exception.class})
    public void doDelete(StudyAdviser adviser) {
        LoginUser user = jwSystemUserDao.queryUserByUserId(adviser.getUserNumber());
        if(user != null && StringUtils.isNotEmpty(user.getId())) {
            jwSystemUserDao.updateUserStat(Long.valueOf(user.getId()), Constants.USER_INVALID_STATE);//更新用户状态为失效
        }
        adviser.setStatus("0");
        adviserDao.update(adviser);
    }

    /**
     * 根据用户唯一账号查询顾问信息
     * @param userNumber
     * @return
     */
    public StudyAdviser findByUserNumber(String userNumber) {
        return adviserDao.findByUserNumber(userNumber);
    }

    /**
     *
     * @param adviser
     * @param uid
     */
    @Transactional(rollbackFor = {Exception.class})
    public void modifyPwd(StudyAdviser adviser, Long uid) {
        if(StringUtils.isNotEmpty(adviser.getPassword())) {
            String passwordEncode = MD5Util.MD5Encode(adviser.getPassword(), "utf-8");//密码加密
            adviser.setPassword(passwordEncode);
        }
        JwSystemUser user = jwSystemUserDao.get(uid);
        user.setPassword(adviser.getPassword());
        this.adviserDao.update(adviser);
        this.jwSystemUserDao.update(user);
    }
}
